<?php
namespace Home\Controller;
use Think\Controller;

class NewsController extends CommonController {

    //显示好友/自己的动态 进行动态回复/点赞/删除(只有自己的可以删除)
    public function index() {
        // var_dump($_SESSION);
        $this->assign('test', '动态广场');
        $this->assign('ajaxurl', U('News/ajaxpush'));
        $this->assign('yesstr', '推送成功!');
        $this->assign('nostr', '推送失败!');
        $this->assign('emptystr', '内容不能为空!');
        $this->assign('userid', $_SESSION['home']['uid']);
        $this->assign('nickname', $_SESSION['home']['nickname']);
        $this->display();
    }

    //实现动态瀑布流需要使用方法
    public function ajaxpull() {
        //只有使用ajax访问的时候 才是被允许的
        if (IS_AJAX) {
            //得到需要传输的数据的数量
            $num = $_POST['page'];
            //得到用户的ID
            $data['user_id_a'] = $_SESSION['home']['uid'];
            //扫描好友关系表
            $ring = M('ring');
            //得到好友的ID
            $res = $ring->field('user_id_b')->where($data)->select();
            if (!$res) {
                $res = array();
            }
            $noid = array_column($res, 'user_id_b');
            array_push($noid, $_SESSION['home']['uid']);
            $noid = implode(',', $noid);
            // echo $noid;
            // die;
            //得到好友和自己的信息
            $news = M('news');
            $info = $news
                ->field('zn.news_id,zn.turn_content,zn.user_id,zn.content,zn.time,zn.isturn,zn.news_zan,zu.nickname as zunickname,yu.nickname as yunickname,zu.photo,zu.user_id as zui,yu.user_id as yui')
                ->where('zu.user_id  IN (' . $noid . ')')
                ->table('( SELECT zn1.user_id AS zn1id,zn2.user_id AS zn2id FROM zz_news AS zn1,zz_news AS zn2 WHERE ( zn1.isturn = zn2.news_id ) GROUP BY zn1id ) AS t')
                ->join('zz_user_info AS zu ON zu.user_id = t.zn1id')
                ->join('zz_user_info AS yu ON yu.user_id = t.zn2id')
                ->join('zz_news AS zn ON zn.user_id = t.zn1id')
                ->page($num, 5)
                ->order('zn.time DESC')
                ->select();
            // var_dump($info);
            // echo $news->_sql();die;
            //纠正转发BUG   现修正 :yunickname yui
            foreach ($info as $key => $val) {
                //得到原动态的ID
                $datanick['news_id'] = $info[$key]['isturn'];
                $newsfornick         = M('news');
                $yunickname          = $newsfornick
                    ->field('zz_user_info.nickname,zz_user_info.user_id')
                    ->where($datanick)
                    ->join('zz_user_info ON zz_user_info.user_id = zz_news.user_id')
                    ->select();
                // echo $newsfornick -> _sql();
                // var_dump($yunickname);die();
                $info[$key]['yunickname'] = $yunickname[0]['nickname'];
                $info[$key]['yui'] = $yunickname[0]['user_id'];
            }
            //将查询结果转换成json格式
            $this->ajaxReturn($info);
        } else {
            $this->error('非法操作');
        }
    }

    //动态发布需要调用的ajax脚本
    public function ajaxpush() {
        // var_dump($_POST);
        //得到用户上传的图片的地址
        if ($_POST['pd'][0] == '') {
            echo 'emptystr';
            die;
        }
        $regimg = '/<img .*?\/>/';
        preg_match_all($regimg, $_POST['pd'][0], $arr);
        //排除百度自带的表情
        $arrpath = array();
        foreach ($arr[0] as $k => $v) {
            $findme = 'baidu';
            $pos    = strpos($v, $findme);
            if ($pos) {
                unset($arr[0][$k]);
            } else {
                var_dump($v);
                //准备图片上传路径的正则
                $regpath = '/\/Uploads\/image\/.*?\"/';
                preg_match($regpath, $v, $arrpath[]);
            }
        }
        // var_dump($arrpath);
        //处理得到的数组
        // $arrsave = array();
        // foreach ($arrpath as $key => $val) {
        //     foreach ($val as $k => $v) {
        //         if ($k == 0) {
        //             unset($arrpath[$key][$k]);
        //         } else {
        //             $arrsave[] = $v;
        //         }
        //     }
        // }
        $arrsave = array_column($arrpath, 0);
        foreach ($arrsave as $key =>  $value) {
            $arrsave[$key] = rtrim($value,'"');
        }
        // var_dump($arrsave);
        //将图片保存到贴图相册中
        $photos = M('photos');
        $albums = M('albums');

        //得到当前用的贴图相册的ID
        $res = $albums->field('albums_id')->where("`albums_type` ='0' AND `user_id`='" . $_SESSION['home']['uid'] . "'")->select();
        // echo $albums->_sql();
        $albums_id = $res[0]['albums_id'];
        foreach ($arrsave as $v) {
            $data['albums_id']   = $albums_id;
            $data['photos_name'] = date('Ymd_His');
            $data['photos_info'] = 'Ta很懒~什么都没写~';
            $data['photos_zan']  = 0;
            $data['photos_path'] = $v;
            $data['time']        = time();
            $photos->create($data);
            $photos->add();
        }

        //将动态存入数据库
        $news = M('news');
        //收集数据
        $data['user_id'] = $_SESSION['home']['uid'];
        $data['content'] = $_POST['pd'][0];
        $data['time']    = time();
        $news->create($data);
        $res = $news->add();
        // echo $news -> _sql();die;
        if ($res) {
            $isturn['isturn'] = $res;
            $news->where('news_id =' . $res)->save($isturn);
            echo $res;
        } else {
            echo 'no';
        }

    }

    //动态回复需要调用的ajax脚本
    public function ajaxreply() {
        //这里请使用ajax访问
        if (IS_AJAX) {
            $data['user_id'] = $_SESSION['home']['uid'];
            $data['news_id'] = $_POST['id'];
            $data['content'] = $_POST['content'];
            $data['time']    = time();
            //实例化news_bb
            $news_bb = M('news_bb');
            $res     = $news_bb->add($data);
            if ($res) {
                echo $res;
            }
        }
    }

    //动态点赞需要调用的ajax脚本
    public function ajaxlike() {
        if (IS_AJAX) {
            //实例化News类
            // var_dump($_POST);
            $data['news_id'] = $_POST['id'];
            $news            = M('news');
            $res             = $news->where($data)->setInc('news_zan'); // 动态的赞加1
            if ($res) {
                echo 'true';
            }
        } else {
            $this->error('非法操作');
        }
    }

    //得到动态的回复的ajax脚本
    public function ajaxgetbb() {
        if (IS_AJAX) {
            $data['news_id'] = $_POST['id'];
            //实例化news_bb
            $news_bb = M('news_bb');
            $res     = $news_bb
                ->field('zz_news_bb.bb_id,zz_news_bb.user_id,zz_news_bb.content,zz_news_bb.time,zz_user_info.nickname')
                ->join('zz_user_info ON zz_user_info.user_id = zz_news_bb.user_id')
                ->where($data)
                ->order('zz_news_bb.time')
                ->select();
            // echo $news_bb -> _sql();die;
            $this->ajaxReturn($res);
        } else {
            $this->error('非法操作');
        }
    }

    //转发动态使用的ajax
    public function ajaxturn() {
        if (IS_AJAX) {
            //得到转发的数据
            $data['user_id']      = $_SESSION['home']['uid'];
            $data['content']      = $_POST['content'];
            $data['time']         = time();
            $data['turn_content'] = $_POST['turn_content'];
            $data['isturn']       = $_POST['id'];

            //实例化news
            $news = M('news');
            $res  = $news->add($data);
            if ($res) {
                echo 'yes';
            }
        } else {
            $this->error('非法操作');
        }
    }

    //删除回复用的ajax脚本
    public function delbb() {
        if (IS_AJAX) {
            // 实例化news_bb
            $news_bb       = M('news_bb');
            $data['bb_id'] = $_POST['id'];
            $res           = $news_bb->where($data)->delete();
            if ($res) {
                echo 'yes';
            }
        } else {
            $this->error('非法操作');
        }
    }

    //动态删除需要调用的ajax脚本
    public function ajaxdel() {
        // 这里请使用ajax访问
        if (IS_AJAX) {
            //实例化news对象
            $id = $_POST['id'];
            //调用和谐函数 递归删除转发动态
            hexie( array($id) );
            //删除要删除的动态
            //得到
            //实例化news_bb对象
            $news_bb           = M('news_bb');
            $databb['news_id'] = $_POST['id'];
            $resbb             = $news_bb->where($databb)->delete();
            // if ($res && $resbb) {
            echo 'true';
            // }
        } else {
            $this->error('非法操作');
        }
    }

}
